package com.china08.yxyapi.api.home.activity.weike;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestCatalogueRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.db.YxyResBookRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ActivityBookContentController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsRepository yrbcr;

    @Autowired
    private YxyResBookRepository yxyResBookRepository;

    @Autowired
    private YxyHuodongWeikeContestCatalogueRepository huodongContentRepository;

    @ApiOperation(value = "获得目录")
    @RequestMapping(value = "/api_yw/home/activity/tea/bookcontent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        RespModel model = new RespModel();
        List<BookContentRespModel1> bookContent = new ArrayList<BookContentRespModel1>();

        String userId = getProfile(request, response).getId();
        YxyResBook yxyResBook = yxyResBookRepository.findById(reqModel.book);
        Assert.notNull(yxyResBook, "书籍不存在");
        List<YxyResBookContents> bookContents = null;
        BookContentRespModel1 model1 = null;
        // 幼儿园的没有知识点
        if ("00".equals(yxyResBook.getPhaseId())) {
            bookContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(reqModel.book, 1);
            for (YxyResBookContents yxyResBookContents : bookContents) {
                model1 = new BookContentRespModel1();
                model1.id = yxyResBookContents.getId();
                model1.name = yxyResBookContents.getName();
                model1.pId = yxyResBookContents.getpId();
                model1.count1 = huodongContentRepository.countByBookContentIdAndUserId(yxyResBookContents.getId(),
                        userId);
                model1.count = huodongContentRepository.countByBookContentIdAndIsApply(yxyResBookContents.getId(),
                        true);
                bookContent.add(model1);
            }
        } else {
            // 拿到所有的章节
            bookContents = yrbcr.findByBookIdAndIsViewAndPIdOrderByOrderNum(reqModel.book, 1, "0");
            for (YxyResBookContents bookContents2 : bookContents) {
                model1 = new BookContentRespModel1();
                model1.id = bookContents2.getId();
                model1.name = bookContents2.getName();
                model1.pId = bookContents2.getpId();
                model1.count = huodongContentRepository.countByBookContentIdAndIsApply(bookContents2.getId(), true);
                model1.count1 = huodongContentRepository.countByBookContentIdAndUserId(bookContents2.getId(), userId);
                model1.children = getAllChild(bookContents2.getId(), new ArrayList<BookContentRespModel1>(), 0, userId);
                bookContent.add(model1);
            }

        }
        model.bookContent = bookContent;
        model.setMessage("成功");
        return model;
    }

    private List<BookContentRespModel1> getAllChild(String pId, List<BookContentRespModel1> child, Integer count,
            String userId) {
        List<YxyResBookContents> list = yrbcr.findByPIdAndIsViewOrderByOrderNum(pId, 1);
        if (CollectionUtils.isEmpty(list)) {
            if (count == 1) {
                YxyResBookContents bookContents = yrbcr.findById(pId);
                BookContentRespModel1 bookContentRespModel1 = new BookContentRespModel1();
                bookContentRespModel1.id = bookContents.getId();
                bookContentRespModel1.name = bookContents.getName();
                bookContentRespModel1.count = huodongContentRepository
                        .countByBookContentIdAndIsApply(bookContents.getId(), true);
                bookContentRespModel1.count1 = huodongContentRepository
                        .countByBookContentIdAndUserId(bookContents.getId(), userId);
                child.add(bookContentRespModel1);
            }
            return child;
        } else {
            for (YxyResBookContents yxyResBookContents : list) {
                getAllChild(yxyResBookContents.getId(), child, 1, userId);
            }
        }
        return child;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String book;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(book, null), "请填写课本ID");
        }

    }

    protected static class BookContentRespModel1 {
        public String id;
        public String pId;
        public String name;
        public Integer count;
        public Integer count1;
        public List<BookContentRespModel1> children = new ArrayList<>();

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<BookContentRespModel1> getChildren() {
            return children;
        }

        public void setChildren(List<BookContentRespModel1> children) {
            this.children = children;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

        public Integer getCount1() {
            return count1;
        }

        public void setCount1(Integer count1) {
            this.count1 = count1;
        }

    }

    protected static class RespModel extends YxyApiRespModel {

        public List<BookContentRespModel1> bookContent;

        public List<BookContentRespModel1> getBookContent() {
            return bookContent;
        }

        public void setBookContent(List<BookContentRespModel1> bookContent) {
            this.bookContent = bookContent;
        }

    }

}
